package eva.savant.business.compras.view

import eva.savant.business.compras.service.TSCompra
import eva.savant.businessobject.cm.Librocompras
import eva.savant.businessobject.gl.*
import eva.savant.core.view.Bean

import javax.faces.bean.ManagedBean
import javax.faces.bean.SessionScoped
import javax.annotation.PostConstruct
import javax.faces.convert.Converter

@SessionScoped
@ManagedBean(name = 'ITLibroCompras')
public class ITLibroCompras extends Bean {

  Date dateIni, dateEnd; def sum

  TSCompra tsCompra

  @PostConstruct
  public void cleanEntity() {

    if(dateIni==null) dateIni = dateEnd= new Date()

    tsCompra= (TSCompra) getSpringBean('tsCompra')


    entity= new Librocompras(); init()

    entity.fecha= new Date()
  }

  public List findLsEntity() {

    dateIni= toStartDay(dateIni); dateEnd= toMidnight(dateEnd);

    sum= get(Librocompras,'sum(importe), sum(iva): fecha between ? and ?', dateIni, dateEnd); sum[0]= round(sum[0],2)

    return find(Librocompras,'fecha between ? and ? order by fecha', dateIni, dateEnd)
  }

  public void editEntity(Object id) {

    entity= get(Librocompras, id)
  }

  public boolean mergeEntity(Object entity) {

    return tsCompra.mergeLibroCompra((Librocompras)entity)
  }

  public List complete(String value) { return completeLike(Persona,'nombre',value) }

  public Converter getConverter() { return converterLike(Persona) }

  public void buscarNIT() { def n= get(Nit, entity.nit); if(n != null) entity.razonsocial= n.razon }

}